import Vue from 'vue'
import Vuex from 'vuex'

import user from '@/store/modules/user'
import setting from '@/store/modules/setting'

// 安装Vuex
Vue.use(Vuex)

// 创建Vuex 仓库实例
const vuex = new Vuex.Store({
  strict: true,
  modules: {
    user,
    setting
  },
  state: {
    key: 999,
    username: '张三'
  },
  getters: {
    addUsernameSuffix (state, s) {
      return state.username + ' 的名字 ' + s.key
    }
  },
  mutations: {
    increase (state, v) {
      console.log(`v = ${v}`)
      state.key++
    },
    decrease (state) {
      state.key--
    },
    update (state, {
      name,
      age
    }) {
      console.log(name)
      console.log(age)
    },
    updateKey (state, v) {
      state.key = v
    }
  },
  actions: {
    increaseAsync (context) {
      setTimeout(() => context.commit('increase'), 1000)
    }
  }
})

export default vuex
